<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/fragment::top">

</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <div th:replace="common/fragment::leftmenu"></div>
    <!--End 左侧导航-->

    <!--头部信息-->
    <div th:replace="common/fragment::header"></div>
    <!--End 头部信息-->

    <!--页面主要内容-->
    <main class="lyear-layout-content">
      <div class="container-fluid">
        <div class="row show-grid col-md-12">
          <div class="col-md-4">
            <label class="col-md-4" for="purchoice" style="margin: 5px 0">采购编号</label>
            <span class="form-control" id="purchoice" th:text="${purIn.purPO.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" for="supplier" style="margin: 5px 0">供应商</label>
            <span class="form-control" id="supplier"
                  th:text="${purIn.purPO.supplierPO.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">要货日期</label>
            <span class="form-control"
                  th:text="${#dates.format(purIn.purPO.need, 'yyyy-MM-dd')}"></span>
          </div>
        </div>
        <div class="divider"></div>
        <div class="row show-grid col-md-12">
          <div class="col-md-4">
            <input hidden="hidden" id="purInId" th:value="${purIn.id}">
            <label class="col-md-4" style="margin: 5px 0">入库编号</label>
            <span class="form-control" th:text="${purIn.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">编制日期</label>
            <span class="form-control"
                  th:text="${#dates.format(purIn.createTime, 'yyyy-MM-dd')}"></span>
          </div>

          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">责任人</label>
            <span class="form-control" th:text="${purIn.userPO.username}"></span>
          </div>
          <div class="divider"></div>
          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">状态</label>
            <span class="form-control" th:text="${purinstatus.get(purIn.status)}"></span>
          </div>
        </div>
      </div>


      <!--入库明细-->
      <div id="detail-table">
        <table class="table table-bordered">
          <thead id="goodheader">
          <tr>
            <!--                <th>序号</th>-->
            <th hidden="hidden">明细id</th>
            <th hidden="hidden">商品id</th>
            <th>商品名称</th>
            <th>商品型号</th>
            <!--    <th>角色图标</th>-->
            <th>商品规格</th>
            <!--                                            <th>包装</th>-->
            <th>入库数量</th>
            <!--    <th>操作</th>-->
          </tr>
          </thead>
          <tbody>
          <!--/*@thymesVar id="purDetailVOList" type="com.examplemartin.jpademo.pojo.vo.purchase.PurDetailVO"*/-->
          <tr class="detail" th:each="detail:${purIn.purInDetailPOList}" >

            <td class="detailid" hidden="hidden" th:text="${detail.id ne null?detail.id:'-999'}">
              明细id
            </td>
            <td class="goodid" hidden="hidden" th:text="${detail.goodPO.id}">
              商品id
            </td>
            <td th:text="${detail.goodPO.name}">商品名称</td>
            <td th:text="${detail.goodPO.spec}">商品型号</td>
            <!--    <th>角色图标</th>-->
            <td th:text="${detail.goodPO.unit}">商品规格</td>
            <td class="oldcount" th:text="${detail.goodcount}">入库数量</td>


          </tr>

          </tbody>
          <div class="col-md-6" id="modify-detail-btn-div">
            <input type="text" id="modify-status-id" hidden="hidden">
            <!--入库单状态下拉菜单-->
            <div class="dropdown col-md-6" >
              <button  class="btn-xs btn-primary dropdown-toggle" type="button"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                状态
                <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" style="min-width:100%;"  >
<!--                <li><a href="-999">任意</a></li>-->
                <li role="separator" class="divider"></li>
                <li th:each="status:${purinstatus}" ><a th:href="${status.getKey()}" th:text="${status.getValue()}">Action</a></li>

              </ul>

            </div>

            <!--END入库单状态下拉菜单-->
            <div class="col-md-6">
              <button
                  class="btn-xs btn-info ajax-post"
                  id="save-status-btn"
                  type="button">确认
              </button>
            </div>


          </div>
        </table>

      </div>
      <!--END添加明细-->
    </main>
    <!--End 页面主要内容-->
  </div>
</div>

<div th:insert="common/fragment::tail-script"></div>

<style>
  .modify {
    color: #ac2925;
    font-weight: bolder;
  }

  .hiddendetail {
    display: none;
  }

  .changedetail {
    color: #ff0000;
    font-weight: bolder;
  }
</style>

<script type="text/javascript">
  $(function () {


    // supplierlist 禁止 状态下拉菜单点击跳转
    let supperid = ''
    $('#modify-detail-btn-div').find('a').each(function (i,e) {
      $(e).click(function (event) {
        status = $(this).attr('href')
        let name = $(this).text()
        $('#modify-detail-btn-div .dropdown-toggle' ).text(name)


        let id = $('.detail').children('.detailid').text()

        $('#modify-status-id').val(status)

        event.preventDefault()
      })
    });

    // 提交修改
    $('#save-status-btn').click(function () {
      let id = $('#purInId').val()

      let status = $('#modify-status-id').val()
      if(status === '-999' || status === ''||status === null){
          confirmMsg("请选择状态")
          return false
      }

      let data ={
        id:id,
        status:status
      }
      post_ajax("/purin/auditPurIn.ajax",data,'/purin/auditpurin.do');
    })



    let flag = true

// 输入数量 为整数
    $('.incount').each(function (i, e) {
      $(e).blur(function () {
        let val = parseInt($(e).val())
        console.log(val)

        if (val == null || val === "Null" || val === '' || isNaN(val)) {
          $(this).val(null)
          return
        }

        if (val < 0) {
          confirmMsg("输入数量必须大于0")
          return false;
        }

        let oldval = $(this).parents('tr').find('.oldcount').text()
        oldval = parseInt(oldval)
        if (parseInt(val) >= 0 && parseInt(val) !== oldval) {
          $(e).parents('tr').addClass('changedetail')
        } else {
          $(e).parents('tr').removeClass('changedetail')
        }
        $(this).val(val)

      });
    });

// 保存订单
    $('#save-detail-btn').click(function () {
      let purInId = $('#purInId').val()
      // 明细

      // 循环有变化的数值
      let purInDetailPOList = []
      $('.changedetail').each(function (i, e) {
        let detailid = $(this).children('.detailid').text()

        let element = null
        if (detailid === '-999') {
          let goodcount = $(this).find('.incount').val()
          goodcount = parseInt(goodcount)
          let goodid = $(this).children('.goodid').text()
          goodid = parseInt(goodid)
          element = {
            goodPO: {id: goodid},
            goodcount: goodcount
          }
        } else {
          let detailid = $(this).children('.detailid').text()
          detailid = parseInt(detailid)
          let oldval = $(this).children('.oldcount').text()
          oldval = parseInt(oldval)
          let newval = $(this).find('.incount').val()
          newval = parseInt(newval)
          element = {
            id: detailid,
            goodcount: newval
          }
        }
        purInDetailPOList.push(element)
      });
      // 生成json
      let data = {
        id: purInId,
        purInDetailPOList: purInDetailPOList
      }

      console.log(data)
      // 发送ajax
      let url = '/purin/auditpurin.do'
      post_ajax("/purin/editpurin.ajax", data, url)

    });

  });
</script>
</body>
</html>